package com.beself.controller;

import com.beself.entity.TMine;
import com.beself.entity.TProblem;
import com.beself.service.MineService;
import com.beself.until.RestJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Member;
import java.util.List;

@Controller
@RequestMapping("api/mine")
public class MineController {

    @Resource
    private MineService mineService;

    @Resource
    private RestJson restJson;

    /**
     * 根据用户的序号查询  指定用户的信息
     *
     * @param uId
     * @return
     */
    @RequestMapping("/findMine")
    @ResponseBody
    public RestJson findMine(@RequestParam("uId") Integer uId) {

        TMine tMine = mineService.findMine(uId);
        if (tMine != null) {
            restJson = new RestJson<>(true, "成功", tMine);
        } else {
            restJson = new RestJson<>(false, "失败", null);
        }
        return restJson;
    }
    /**
     * 用户信息更新TChallengeMapper
     */
    @RequestMapping("updateMine")
    @ResponseBody
    public RestJson updateMine(TMine tMine, @RequestParam("uploadname") MultipartFile headimg, HttpServletRequest request) {
        String filename = "";
        if (headimg != null) {
            String realPath = request.getRealPath("/upload");
            File file = new File(realPath);
//            if (!file.exists()){
//                file.mkdirs();
//            }
            //System.out.println(realPath);

            filename = headimg.getOriginalFilename();

            if (filename != null && filename.trim().length() > 0) {

                String url = "http://10.9.20.181:8100/upload/"+filename;
                tMine.setmImg(url);

                System.out.println(url);
            }
            System.out.println(filename);
            try {
                headimg.transferTo(new File(file + "/" + filename));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean flag = mineService.updateMine(tMine);

        if (flag) {
            restJson = new RestJson(true, "成功", null);
        } else {
            restJson = new RestJson(false, "失败", null);
        }
        return restJson;
    }
    /**
     * 反馈信息添加
     * @param tProblem
     * @return
     */
    @RequestMapping("insertProblem")
    @ResponseBody
    public RestJson insertProblem(TProblem tProblem, @RequestParam("updatename") MultipartFile headimg, HttpServletRequest request) {
        String filename = "";
        if (headimg != null) {
            String realPath = request.getRealPath("/upload");
            File file = new File(realPath);
//            if (!file.exists()){
//                file.mkdirs();
//            }
            filename = headimg.getOriginalFilename();
            if (filename != null && filename.trim().length() > 0) {
                String url = "http://10.9.20.181:8100/upload/"+filename;
                tProblem.setpImg(url);
            }
            System.out.println(filename);
            try {
                headimg.transferTo(new File(file + "/" + filename));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Boolean flag = mineService.insertProblem(tProblem);

        if (flag) {
            restJson = new RestJson(true, "成功", null);
        } else {
            restJson = new RestJson(false, "失败", null);
        }
        return restJson;
    }

    /**
     * 普通账号----->会员账号  通过登录账号查询用户是否事会员用户
     */
    @RequestMapping("updateIsmember")
    @ResponseBody
    public RestJson updateIsmember(TMine mine) {
        Boolean flag = mineService.updateIsmember(mine);
        //是会员用户
        if (flag) {
            restJson = new  RestJson(true, "成功", null);
        //不是会员用户
        } else {
            restJson = new  RestJson(false, "失败", null);
        }
        return restJson;
    }

    //会员开通  信息展示
    @RequestMapping("showSuperUser")
    @ResponseBody
    public RestJson showSuperUser(TMine tMine) {

        TMine mine = mineService.showSuperUser(tMine);
        //是会员用户
        if (mine.getmIsmember() == 0) {
            restJson = new  RestJson(true, "会员用户", mine);
            //不是会员用户
        } else {
            restJson = new  RestJson(false, "普通用户", mine);
        }
        return restJson;
    }

    //查询会员套餐
    @RequestMapping("showMember")
    @ResponseBody
    public RestJson showMember() {
        List<Member> memberList = mineService.showMember();
        if (memberList != null) {
            restJson = new RestJson(true, "成功", memberList);
        } else {
            restJson = new RestJson(false, "失败", null);
        }
        return restJson;
    }
}
